package com.chaodriod.common.utils

import android.content.Context
import android.text.TextUtils
import android.widget.Toast

/**
 * Created by CHEN on 2017/1/11.
 */

object ToastUtils {
    private var oldMsg: String? = null
    private var time: Long = 0

    @JvmOverloads
    fun show(context: Context?, msg: String, duration: Int = Toast.LENGTH_SHORT) {
        if (context == null) {
            return
        }
        if (TextUtils.isEmpty(msg)) {
            return
        }
        if (TextUtils.isEmpty(oldMsg) || msg != oldMsg) { // 当显示的内容不一样时，即断定为不是同一个Toast
            Toast.makeText(context, msg, duration).show()
            time = System.currentTimeMillis()
        } else {
            // 显示内容一样时，只有间隔时间大于2秒时才显示
            if (System.currentTimeMillis() - time > 2000) {
                Toast.makeText(context, msg, duration).show()
                time = System.currentTimeMillis()
            }
        }
        oldMsg = msg
    }

}
